<?php

namespace Store\Controller;
use Think\Controller;
class StoreController extends CommonController {
    function info(){
    	$storeArray  =   $this->storeArray;
    	$storeInfo	= D('store');
      	$current = $storeInfo->where('id='.$storeArray['id'])->find();
		$this->assign ('current',$current );
		$this->display();
    }
    
    public function store_Save(){
    	$storeArray  =   $this->storeArray;
		$data=I('post.');
		if($data['tui_time']<7){
            $this->ajax_r(0,"退换货时间不能少于7天");
        }
		if($data['extraction']){
			$data['extraction']=$data['extraction']/100;
		}
		if($data['spread']=='on'){
			$data['spread']=1;
		}else{
			$data['spread']=0;
		}
		if($data['recommend']=='on'){
			$data['recommend']=1;
		}else{
			$data['recommend']=0;
		}
		if($data['status']=='on'){
			$data['status']=1;
		}else{
			$data['status']=0;
		}
        if($data['is_tixian']=='on'){
            $data['is_tixian']=1;
        }else{
            $data['is_tixian']=0;
        }
        if($data['is_delivery']=='on'){
            $data['is_delivery']=1;
        }else{
            $data['is_delivery']=0;
        }
		$storeInfo	= D('store');
		if($storeArray['id']){
			$data['id']=$storeArray['id'];
			$storeInfo->save($data);
            $this->ajax_r(1,"修改成功",array('url' => U('Store/info')));
		}
	}
	
	public function store_Save_ali(){
		$storeArray  =   $this->storeArray;
		$data=I('post.');
		$ali_moblie_code=session('ali_moblie_code');
		if($ali_moblie_code==$data['code']&&$ali_moblie_code){
			if($data['id']){
				$info['id']=$data['id'];
				$info['ali_number']=$data['ali_number'];
				$info['ali_name']=$data['ali_name'];
				$storeInfo	= D('store');
				$storeInfo->save($info);
				session('ali_moblie_code',null);
                $this->ajax_r(1,"更新成功",array('url' => U('Shop/info')));
			}else{
                $this->ajax_r(0,"缺少值，请刷新重试");
			}
		}else{
            $this->ajax_r(0,"手机验证码错误");
		}
	}
	
	public function template(){
		$storeArray  =   $this->storeArray;
		$store_template =M('store_template');
		$store_template_info=$store_template->where('store_id='.$storeArray['id'])->find();
		$this->assign('store_template_info',$store_template_info);
		//获取banners
		$banners=explode(',',$store_template_info['banners']);
		$url=explode(',',$store_template_info['url']);
		$this->assign('url',$url);
		$this->assign('banners',$banners);
		//获取模版
		$dir="./themes/wnsc/Home/Store/template/";
		$file=treeDirectory($dir);
		$data=array();
		$i=0;
		foreach($file as $key=>$v){
			if(strstr($v,"index.json")){
				$json_string = file_get_contents($v);
				$data[$i]=json_decode($json_string,true);
				$i++;
			}
		}
		$this->assign('data',$data);
		$this->display();
	}
	
	public function template_save(){
		$data=I('post.');
		$newdata['logo']=$data['logo'];
		$newdata['banner']=$data['banner'];
		$newdata['portrait']=$data['portrait'];
		$newdata['template']=$data['template'];
		if($data['banner1']){
			$newdata['banners']=$data['banner1'];
		}
		if($data['banner2']){
			$newdata['banners']=$newdata['banners'].','.$data['banner2'];
		}
		if($data['banner3']){
			$newdata['banners']=$newdata['banners'].','.$data['banner3'];
		}
		if($data['banner4']){
			$newdata['banners']=$newdata['banners'].','.$data['banner4'];
		}
		if($data['banner5']){
			$newdata['banners']=$newdata['banners'].','.$data['banner5'];
		}
		
		if($data['url1']){
			$newdata['url']=$data['url1'];
		}
		if($data['url2']){
			$newdata['url']=$newdata['url'].','.$data['url2'];
		}
		if($data['url3']){
			$newdata['url']=$newdata['url'].','.$data['url3'];
		}
		if($data['url4']){
			$newdata['url']=$newdata['url'].','.$data['url4'];
		}
		if($data['url5']){
			$newdata['url']=$newdata['url'].','.$data['url5'];
		}
	
		
		$storeArray  =   $this->storeArray;
		$store_template =M('store_template');
		$store_template_info=$store_template->where('store_id='.$storeArray['id'])->find();
		if($store_template_info){
			$rs=$store_template->where('store_id='.$storeArray['id'])->save($newdata);
		}else{
			$newdata['store_id']=$storeArray['id'];
			$rs=$store_template->add($newdata);
		}
		
		if($rs){
            $this->ajax_r(1,"更新成功");
		}else{
            $this->ajax_r(0,"无数据更新");
		}
		
	}
	
	public function goodsRecommendClass(){
		$ticket_type=M('ticket_type');
		$category_list_1=$ticket_type->where('bid=0')->select();
		$this->assign('category_list_1',$category_list_1);
		$storeArray  =   $this->storeArray;
		$store_class=M('store_class');
		$shop=M('shop');
		$goods_recommend_class=$store_class->where('store_id='.$storeArray['id'])->select();
		foreach($goods_recommend_class as $key=>$value){
			$shop_ids=explode(',',$value['shop_ids']);
			$goods_recommend_class[$key]['shop_list']=array();
			foreach($shop_ids as $k=>$v){
				
				$shop_info=$shop->field('image,title')->find($v);
				$goods_recommend_class[$key]['shop_list'][$k]=$shop_info;
			}
		}
		
		$this->assign('goods_recommend_class',$goods_recommend_class);
		$this->display();
	}
	
	public function getgoodsrecommendclass(){
		$storeArray  =   $this->storeArray;
		$store_class=M('store_class');
		$shop=M('shop');
		$id=I('post.class_id');
		
		if($id){
			$rs=$store_class->find($id);
			$shop_ids=explode(',',$rs['shop_ids']);
			if($shop_ids){
				foreach($shop_ids as $k=>$v){
					$shop_info=$shop->field('id,image,title')->find($v);
					if($shop_info){
						$shop_infos[$k]=$shop_info;
					}
				}
				$rs['shop_list']=$shop_infos;
			}
			$this->ajaxReturn($rs);
		}
	}
	public function savegoodsrecommendclass(){
		
		IF($_POST){
			$storeArray  =   $this->storeArray;
			$store_class=M('store_class');
			if(I('post.class_id')){
				$data['id']=I('post.class_id');
				$data['store_id']=$storeArray['id'];
				$data['class_name']=I('post.class_name');
				$data['shop_ids']=I('post.goods_id_str');
				$data['sort']=I('post.sort');
				$rs=$store_class->save($data);
				if($rs){
                    $this->ajax_r(1,"保存成功");
				}else{
                    $this->ajax_r(0,"保存失败");
				}
			}else{
				$data['store_id']=$storeArray['id'];
				$data['class_name']=I('post.class_name');
				$data['shop_ids']=I('post.goods_id_str');
				$data['sort']=I('post.sort');
				$rs=$store_class->add($data);
				if($rs){
                    $this->ajax_r(1,"添加成功");
				}else{
                    $this->ajax_r(0,"添加失败");
				}
			}
		}
	}
	
	public function delgoodsrecommendclass(){
		IF($_POST){
			if(I('post.class_id')){
				$store_class=M('store_class');
				$rs=$store_class->delete(I('post.class_id'));
			}
			if($rs){
                $this->ajax_r(1,"删除成功");
			}else{
                $this->ajax_r(0,"删除失败");
			}
		}
	}
	public function searchgoods(){
		IF($_POST){
			$storeArray  =   $this->storeArray;
			$data=I('post.');
			if($data['category_id']!=0){
				$where['_string']='FIND_IN_SET('.$data['category_id'].',type_id)';
			}
			if($data['goods_name']){
				$data['goods_name']='%'.$data['goods_name'].'%';
				$where['title'] = array('like',$data['goods_name']);
				$shop=M('shop');
				$where['ishidden']=0;
				$where['store_id']=$storeArray['id'];
				$shop_list=	$shop->where($where)->select();
			}else{
				$shop_list='';
			}
			
			$this->ajaxReturn($shop_list);
			
		}
	}
	public function upload(){
		if(I('get.isthumb')){
            $width  =   I('get.width','400');
            $height =   I('get.height','400');
        }else{
            $width  =   I('get.width');
            $height =   I('get.height');
        }
        if (!empty($_FILES)) {
            import("@.ORG.UploadFile");
            $upload = new \Think\Upload();
            $upload->maxSize = 2048000;
            $upload->allowExts = array('jpg','jpeg','gif','png','xls');
            $upload->savePath = "/";
            $upload->thumb = true; //设置缩略图
            $upload->imageClassPath = "@.ORG.Image";
            $upload->saveRule = uniqid; //上传规则
            $upload->thumbRemoveOrigin = true; //删除原图
            $info   =   $upload->upload();
            if(!$info) {
                echo 'error'.$upload->getError();// 上传错误提示错误信息
            }else{
                foreach($info as $file){
                    $path='/Uploads'.$file['savepath'].$file['savename'];
                    $name   =   $_SERVER["DOCUMENT_ROOT"].$path;
                    $imageArray =   getimagesize($name);
                    if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
                        $save_path=$_SERVER['DOCUMENT_ROOT'].$path;
                        $thumb_path=$_SERVER['DOCUMENT_ROOT'].'/Uploads'.$file['savepath'].'thumb_'.$file['savename'];
                        $image = new \Think\Image(); 
                        $image->open($save_path);
                        $image->thumb($width, $height)->save($save_path);
                    }
                    
                }
            }
           
            $data['saveURL']=$path;
	        $data['state']='上传成功！';
	        $data['code']=1;
	       	$this->ajaxReturn($data);
           
       	}
       	
	}
    /**
     * 获取省列表
     */
    public function getProvince()
    {
    	$province_list = getProvinceList();
    	$this->ajaxReturn($province_list);
    }

    /**
     * 获取城市列表
     *
     * @return Ambigous <multitype:\think\static , \think\false, \think\Collection, \think\db\false, PDOStatement, string, \PDOStatement, \think\db\mixed, boolean, unknown, \think\mixed, multitype:, array>
     */
    public function getCity()
    {
        
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_list = getCityList($province_id);
       
        $this->ajaxReturn($city_list);
       
    }

    /**
     * 获取区域地址
     */
    public function getDistrict()
    {
       
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $district_list = getDistrictList($city_id);
        
        $this->ajaxReturn($district_list);
       
    }

    /**
     * 获取选择地址
     *
     * @return unknown
     */
    public function getSelectAddress()
    {
        $address = new Address();
        $province_list = getProvinceList();
        $province_id = isset($_POST['province_id']) ? $_POST['province_id'] : 0;
        $city_id = isset($_POST['city_id']) ? $_POST['city_id'] : 0;
        $city_list = getCityList($province_id);
        $district_list =getDistrictList($city_id);
        $data["province_list"] = $province_list;
        $data["city_list"] = $city_list;
        $data["district_list"] = $district_list;
        $this->ajaxReturn($data);
    }
   
   	public function store_edit_ali(){
   		$id=$_GET['id'];
   		$storeInfo	= D('store');
      	$current = $storeInfo->find($id);
		$this->assign ('current',$current );
		$this->display();
   	}
   	
}